<?php
    session_start();

    include_once "site/config.php";

    include CORE_DIR."/index.php";

    include_once "site/models/profile.php";


    $TARGET = new target( $_SERVER['REQUEST_URI'], $tmp);
    $user   = new profile( (isset($_SESSION['user_id']) ? (int)$_SESSION['user_id'] : 0), $TARGET);
    $TARGET->set_user($user);


    /***************************************************************
    * Populate error/notify constants (if set)
    */
    if ( isset($_SESSION['ERROR_STR']) ) {
            define('ERROR_STR', $_SESSION['ERROR_STR']);
    } elseif ( isset($_SESSION['NOTIFY_STR']) ) {
            define('NOTIFY_STR', $_SESSION['NOTIFY_STR']);
    }


    /***************************************************************
    * PROCESS REQUEST
    */
    $TARGET->call_controller();



    include "{$TARGET->template_root}/{$TARGET->template}";



    /***************************************************************
     * Keep track of last action
     */

    $_SESSION['LAST_ACTION'] = $_SERVER['REQUEST_URI'];

    /**
     * Clear down these debug values if set
     *
     * @todo could be nicer! (array containing all 4 items?)
     */

    foreach ( array('ERROR','NOTIFY') as $redirect ) {
        if ( isset($_SESSION["TARGET_PRE_{$redirect}"]) ) {
                unset($_SESSION["TARGET_PRE_{$redirect}"]);
        }

        if ( isset($_SESSION["{$redirect}_STR"]) ) {
                session_unregister("{$redirect}_STR");
        }
    }